package cn.getech.data.development.controller;


import cn.getech.data.development.constant.DataDevelopmentBizExceptionEnum;
import cn.getech.data.development.entity.ModelInfo;
import cn.getech.data.development.model.qo.ModelInfoQO;
import cn.getech.data.development.model.vo.ModelInfoVO;
import cn.getech.data.development.service.ModelInfoService;
import cn.getech.data.intelligence.common.exception.RRException;
import cn.getech.data.intelligence.common.utils.FileUtils;
import cn.getech.data.intelligence.common.utils.PageUtils;
import cn.getech.data.intelligence.common.utils.R;
import cn.getech.data.intelligence.common.validator.ValidatorUtils;
import cn.getech.system.center.annotation.SysLog;
import cn.getech.system.center.constant.OperationModulesEnum;
import cn.getech.system.center.constant.OperationTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * <p>
 * 模型表 前端控制器
 * </p>
 *
 * @author hqz
 * @since 2019-08-22
 */
@Api(tags = "模型相关")
@RestController
@RequestMapping("/bdp/modelInfo")
public class ModelInfoController {

    @Autowired
    ModelInfoService modelInfoService;

//    @ApiOperation("获取所有模型信息，现在只能根据模型名称搜索，分页默认10条/页")
//    @SysLog(value = "获取所有模型信息", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE)
//    @GetMapping("/list")
//    public R modelList(@ApiParam("{'name':'我的模型'}") ModelInfoQO modelInfoQO) {
//        ValidatorUtils.validateEntity(modelInfoQO);
//        modelInfoQO.setOrder("desc");
//        modelInfoQO.setOrderField("id");
//        PageUtils page = modelInfoService.queryPage(modelInfoQO);
//        return R.okWithPage(page);
//    }
//
//    @ApiOperation("上传模型")
////    @SysLog(value = "上传模型")
//    @SysLog(value = "上传模型", type = OperationTypeEnum.UPLOADCODE, modulesType = OperationModulesEnum.BDPMODELCODE)
//    @PostMapping("/upload")
//    public R uploadModelFile(@ApiParam(value = "{name':'模型一','decr':'模型描述'}") @RequestParam(value = "file", required = false) MultipartFile file, ModelInfoVO modelInfoVO) {
//        ValidatorUtils.validateEntity(modelInfoVO);
//        modelInfoService.checkUploadInfo(modelInfoVO);
//        FileUtils.checkFileOnUpload(file, 1);
//        //上传文件到hdfs
//        modelInfoService.uploadFile(file, modelInfoVO);
//        return R.ok("模型上传成功");
//    }
//
//    @ApiOperation("编辑模型")
//    @SysLog(value = "编辑模型", type = OperationTypeEnum.UPLOADCODE, modulesType = OperationModulesEnum.BDPMODELCODE)
//    @PostMapping("/update")
//    public R updateModelFile(@ApiParam(value = "{'id':1,'name':'模型一','decr':'模型描述'}") @RequestParam(value = "file", required = false) MultipartFile file, ModelInfoVO modelInfoVO) {
//        ValidatorUtils.validateEntity(modelInfoVO);
//
//        //验证是否有分析任务正在使用这个模型
////        modelInfoService.checkedAnalysisInfo(modelInfoVO.getId(),2);
//        modelInfoService.checkUpdateInfo(modelInfoVO);
//        FileUtils.checkFileOnUpdate(file, 1);
//        modelInfoService.updateFile(file, modelInfoVO);
//        return R.ok("模型编辑成功");
//    }
//
//    @ApiOperation("获取模型详情信息")
//    @SysLog(value = "获取模型详情信息", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE)
//    @GetMapping("/getModelDetials")
//    public R getModelDetials(@ApiParam(value = "{'id':151}") @RequestParam(value = "id", required = true) String id) {
//        return R.okWithData(modelInfoService.getModelDetials(id));
//    }
//
//    @ApiOperation("删除模型")
//    @SysLog(value = "删除模型", type = OperationTypeEnum.DELETECODE, modulesType = OperationModulesEnum.BDPMODELCODE)
//    @PostMapping("/deleteModel")
//    public R deleteModel(@ApiParam(value = "{'id':151}") @RequestBody ModelInfo modelInfo) {
//        if (null == modelInfo || modelInfo.getId() == null) {
//            throw new RRException(DataDevelopmentBizExceptionEnum.MODEL_IN_MYSQLTABLE_NULL.getMessage());
//        }
//        //验证是否有分析任务正在使用这个模型
//        modelInfoService.checkedAnalysisInfo(modelInfo.getId(), 1);
//        modelInfoService.deleteModle(modelInfo.getId());
//        return R.ok("模型删除成功");
//    }
//
//    @ApiOperation("下载模型")
//    @SysLog(value = "下载模型", type = OperationTypeEnum.DOWNLOADCODE, modulesType = OperationModulesEnum.BDPMODELCODE)
//    @PostMapping("/downloadModel")
//    public void downloadModel(@ApiParam(value = "{'id':151}") HttpServletResponse response, @RequestBody ModelInfo model) throws Exception {
//        if (model == null || model.getId() == null) {
//            throw new RRException(DataDevelopmentBizExceptionEnum.MODEL_IN_MYSQLTABLE_NULL.getMessage());
//        }
//        ModelInfo modelInfo = modelInfoService.getById(model.getId());
//        response.reset();
//        response.setContentType("multipart/form-data");
//        response.setHeader("Content-Type", "application/octet-stream;charset=utf-8");
//        String fileName = String.format("%s.pmml", URLEncoder.encode(modelInfo.getName(), "utf-8"));
//        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
//        response.setHeader("filename", fileName);
//        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
//        response.setCharacterEncoding("UTF-8");//设置编码集,文件名不会发生中文乱码
//        BufferedInputStream bufferedInputStream = null;
//        OutputStream outputStream = null;
//        try {
//            bufferedInputStream = modelInfoService.downloadModel(modelInfo, response);
//            outputStream = response.getOutputStream();
//        } catch (IOException | IllegalArgumentException e) {
//            e.printStackTrace();
//        }
//        byte[] b = new byte[1024];
//        int j;
//        while ((j = bufferedInputStream.read(b)) != -1) {
//            outputStream.write(b, 0, j);
//        }
//        outputStream.flush();
//        outputStream.close();
//        if (bufferedInputStream != null) {
//            bufferedInputStream.close();
//        }
//
//    }
//
//
////    @ApiOperation("获取数据仓库中某个数据库下的所有表格")
////    @SysLog("获取数据仓库中某个数据库下的所有表格")
////    @GetMapping("/getDbTableList")
////    public R tableList() throws Exception {
////        return R.okWithData(modelInfoService.getDbTableList());
////    }
//
//    @ApiOperation("根据用户获取数据仓库中某个数据库下的所有表格")
//    @SysLog(value = "根据用户获取数据仓库中某个数据库下的所有表格", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE)
//    @GetMapping("/getDbTableList")
//    public R dbTableList() {
//        return R.okWithData(modelInfoService.dbTableList(1));
//    }
//
//
//    @ApiOperation("根据选中的数据仓库dbName和tableName获取字段名")
//    @SysLog(value = "根据选中的数据仓库dbName和tableName获取字段名", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE)
//    @GetMapping("/definiteDbTableTree")
//    public R definiteDbTableTree(@ApiParam(value = "{'tableInfoId':50}") @RequestParam(value = "tableInfoId") Integer tableInfoId) {
//        return R.okWithData(modelInfoService.definiteDbTableTree(tableInfoId));
//    }
//
//
//    @ApiOperation("hive查询数据并存入hdfs")
//    @SysLog(value = "hive查询数据并存入hdfs", type = OperationTypeEnum.ADDCODE, modulesType = OperationModulesEnum.BDPMODELCODE)
//    @GetMapping("/modelTrainData")
//    public R modelTrainData(@ApiParam(value = "{'sql':'select * from db.table'}") @RequestParam("sql") String sql) {
//        modelInfoService.modelTrainData(sql);
//        return R.ok();
//    }
}

